define("qunit", function(require){
	var base = require("base");
	var lm = require("login-manager");
	var api = require("api");
	var data = require("data");

	var testApiBase = "http://localhost:8080/web/index.php/";
	api.setBase(testApiBase);


	QUnit.test("base.error", function(assert){
		var message = "api error";
		var apiError = new base.ApiError(message);
		assert.equal(apiError.toString(), message);

		assert.ok(apiError instanceof base.ApiError, "test type ok");
	});

	QUnit.test("api", function(assert){
		assert.equal(api.getBase(), testApiBase, "api base test ok");
		
		var no_user_need_done = assert.async();
		var user_need_but_no_token_done = assert.async();
		var user_need_and_have_token_done = assert.async();
		var token_expires_done = assert.async();

		api.bareCall("qunit/no-user", null, function(json, error){
			assert.ok(json.status == 0, "bare api call ok");
			no_user_need_done();
		});

		lm.updateAccessToken("");
		api.call("qunit/need-user", null, function(json, error){
			assert.ok(error instanceof base.TokenError, "api call with no token ok");
			user_need_but_no_token_done();

			lm.updateAccessToken("123");
			api.call("qunit/need-user", null, function(json, error){
				assert.equal(json.status, 0, "api call with no token ok");
				user_need_and_have_token_done();

				api.call("qunit/token-expires", null, function(json, error){
					assert.ok(error instanceof base.TokenError, "api call with expires token ok");
					token_expires_done();
				});
			});
		});

		var echo = "hello world";

		var with_def_done = assert.async();
		api.withDef("call", "qunit/test", {"data" : echo}).done(function(json){
			assert.equal(json.data, echo, "api.WithDef ok");
			with_def_done();
		});

		var call_ex_done = assert.async();
		api.callEx("qunit/test", {"data" : echo}).done(function(json){
			assert.equal(json.data, echo, "api.callEx ok");
			call_ex_done();
		});

		var bare_call_ex_done = assert.async();
		api.bareCallEx("qunit/test", {"data" : echo}).done(function(json){
			assert.equal(json.data, echo, "api.callEx ok");
			bare_call_ex_done();
		});
	});

	QUnit.test("data", function(assert){
		var echo = "hello world";

		var test_done = assert.async();
		data.call("qunit/test", {"data" : echo}).done(function(json){
			assert.equal(json.data, echo, "data call ok");
			test_done();
		});

		var test_data_error_done = assert.async();
		data.on("error", function(error){
			assert.ok(error instanceof base.TokenError, "data call with error response event ok");
		});
		data.call("qunit/token-expires").fail(function(error){
			assert.ok(error instanceof base.TokenError, "data call with error response ok");
			test_data_error_done();
		});
	});
});